/*
 * This file is Copyright 2002-2006 Loyola University Chicago,
 * Department of Computer Science and the Emerging Technologies Laboratory.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License via
 *    http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.citep.web.admin.users.client;

import com.citep.web.gwt.module.Presentation;
import com.citep.web.gwt.module.BasePresentation;
import com.citep.web.gwt.widgets.ShortcutTable;
import com.citep.web.gwt.widgets.StatusLabel;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.citep.web.gwt.widgets.DynaTable;
import com.citep.web.gwt.widgets.Shortcut;
import com.citep.web.gwt.widgets.DataClickListener;
import com.citep.web.gwt.dto.*;

public class UserListPresentation extends BasePresentation implements Presentation {

	public void displayError(String key, String value) {
		// TODO Auto-generated method stub
		
	}

	public void displayStatus(String msg, int seconds) {
		// TODO Auto-generated method stub
	
	}

	protected UserListApplication application;
	
	protected Label titleLabel;
	protected StatusLabel statusLabel;
	protected VerticalPanel mainPanel;
	protected ShortcutTable shortcuts;
	
	protected DynaTable dataGrid;
	
	protected Button createUserButton;	

	public UserListPresentation() {
		
		
	}
	
	public DynaTable getDataGrid() {
		if(dataGrid == null) {
			dataGrid = new DynaTable(application,  new String [] {"First Name", "Last Name", "Email", "Role", "Last Login"}, "data_grid", "data_grid_header", "data_grid_entry", new String [] {"grid_normal_item", "grid_normal_item","grid_normal_item","grid_small_item", "grid_small_item"}, 10);

			dataGrid.setDataClickListener(new DataClickListener() {
				public void onDataElementClicked(int row, int cell) {
					application.onRowClicked(row);
				}
			});
		}
		
		return dataGrid;
	}

	public void setApplication(UserListApplication application) {
		this.application = application;
	}	
	

	public Label getTitleLabel() {
		if(titleLabel == null) {
			titleLabel = new Label("User List");
			titleLabel.setStyleName("form_title");
		}
		
		return titleLabel;
	}
	
	public StatusLabel getStatusLabel() {
		if(statusLabel == null) {
			statusLabel = new StatusLabel();
			statusLabel.setStyleName("status_label");
			statusLabel.setVisible(false);
		}

		return statusLabel;
	}
	
	protected Button getCreateUserButton() {
		if(createUserButton == null) {
			createUserButton = new Button("Create User");
			createUserButton.setStyleName("form_button");
			createUserButton.addClickListener(new ClickListener() {
				public void onClick(Widget w) {
					String token = HistoryEncoder.encode("user_entry",null);
					History.newItem(token);
				}
			});
		}
		
		return createUserButton;
	}
		
	public void addShortCut(Shortcut s, boolean highlight) {
		shortcuts.addShortcut(s, highlight);
	}
		
	public Widget getMainPanel() {
		if(mainPanel == null) {
			mainPanel = new VerticalPanel();
			mainPanel.setWidth("100%");
			mainPanel.add(getTitlePanel());
			mainPanel.add(getDataGrid());
			mainPanel.add(getStatusLabel());
			
			mainPanel.setCellHorizontalAlignment(getDataGrid(), HasHorizontalAlignment.ALIGN_CENTER);
			mainPanel.setCellHorizontalAlignment(getStatusLabel(), HasHorizontalAlignment.ALIGN_CENTER);
		}
		return mainPanel;
	}
	
	public Widget getContext() {
		
		VerticalPanel p = new VerticalPanel();
		p.setStyleName("table_center");
		p.setSpacing(0);
		
		shortcuts = new ShortcutTable("Categories",10);
		shortcuts.setMoreLink(new Hyperlink("[more]","accounts_list"));
		
		p.add(getCreateUserButton());
		p.add(shortcuts);
		
		return p;
	}	
}
